﻿using DomainService.IService.Sys;
using DomainService.Service.Sys;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.BusinessModel.Input;
using Model.BusinessModel.InPut;
using Model.BusinessModel.OutPut;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;
using OverallAuth_WebApi.PlugInU;
using Utility;
using Utility.Enum;

namespace OverallAuth_WebApi.Controllers.Sys
{
    /// <summary>
    /// 角色模块
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    [ApiExplorerSettings(GroupName = nameof(ModeuleGroupEnum.SysRole))]
    public class SysRoleController : BaseController
    {
        #region 构造实列化

        /// <summary>
        /// 角色服务
        /// </summary>
        public ISysRoleService _roleService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="roleService"></param>
        public SysRoleController(ISysRoleService roleService)
        {
            _roleService = roleService;
        }

        #endregion

        #region 接口

        /// <summary>
        /// 获取树形角色数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<SysRoleOutPut> GetAllRoleTreeList()
        {
            ReceiveStatus<SysRoleOutPut> receiveStatus = new();
            var list = _roleService.GetAllRoleTreeList(GetLoginUserMsg());
            receiveStatus.data = list;
            return receiveStatus;
        }

        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="sysRole">角色数据</param>
        [HttpPost]
        public ReceiveStatus Insert(SysRole sysRole)
        {
            sysRole.CreateTime = DateTime.Now;
            sysRole.CreateUser = GetLoginUserMsg().UserId;
            return _roleService.Insert(sysRole);
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="sysRole">角色数据</param>
        [HttpPost]
        public ReceiveStatus Update(SysRole sysRole)
        {
            return _roleService.Update(sysRole);
        }

        /// <summary>
        /// 设置角色是否启用
        /// </summary>
        /// <param name="updateIsOpenInPut"></param>
        /// <returns></returns>
        [HttpPost]
        public ReceiveStatus SetRoleIsOpen(UpdateIsOpenInPut updateIsOpenInPut)
        {
            return _roleService.SetRoleIsOpen(updateIsOpenInPut);
        }

        /// <summary>
        /// 获取树形结构角色数据
        /// </summary>
        /// <param name="corporationKey">公司key</param>
        /// <param name="systemKey">系统key</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<TreeOutPut> GetRoleTree(string corporationKey, string? systemKey)
        {
            return _roleService.GetRoleTree(corporationKey, systemKey);
        }

        /// <summary>
        /// 保存角色权限--菜单权限
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ReceiveStatus SaveRoleAuthority(RoleMenuInput roleMenuExend)
        {
            return _roleService.SaveRoleAuthority(roleMenuExend, GetLoginUserMsg().UserId ?? string.Empty);
        }

        /// <summary>
        /// 保存角色权限--按钮权限
        /// </summary>
        /// <param name="roleButtonExend">角色和按钮模型</param>
        [HttpPost]
        public ReceiveStatus SaveRoleButtonAuthority(RoleButtonInput roleButtonExend)
        {
            return _roleService.SaveRoleButtonAuthority(roleButtonExend, GetLoginUserMsg().UserId ?? string.Empty);
        }

        /// <summary>
        /// 绑定角色用户
        /// </summary>
        /// <param name="roleUserInput">角色和用户模型</param>
        [HttpPost]
        public ReceiveStatus BindRoleUser(RoleUserInput roleUserInput)
        {
            return _roleService.BindRoleUser(roleUserInput, GetLoginUserMsg().UserId ?? string.Empty);
        }

        /// <summary>
        /// 根据角色获取所属菜单key
        /// </summary>
        /// <param name="roleId">角色id</param>
        /// <param name="isContainSubordinate">是否包含下级角色</param>
        /// <returns>返回角色所属菜单key</returns>
        [HttpGet]
        public ReceiveStatus<MenuOrRoleOutPut> GetMenuKeyByRoleId(int roleId, bool isContainSubordinate)
        {
            return _roleService.GetMenuKeyByRoleId(roleId, isContainSubordinate, GetLoginUserMsg().CorporationKey ?? string.Empty);
        }

        /// <summary>
        /// 根据角色id获取权限按钮
        /// </summary>
        /// <param name="roleId">角色id</param>
        /// <param name="isContainSubordinate">是否包含下级角色</param>
        /// <returns>返回角色所属按钮key</returns>
        [HttpGet]
        public ReceiveStatus<ButtonOrRoleOutPut> GetButtonKeyByRoleId(int roleId, bool isContainSubordinate)
        {
            return _roleService.GetButtonKeyByRoleId(roleId, isContainSubordinate, GetLoginUserMsg().CorporationKey ?? string.Empty);
        }
        #endregion
    }
}
